package com.haizhi.crm.api.controller;

import com.alibaba.fastjson.JSON;
import com.arangodb.ArangoCollection;
import com.arangodb.ArangoCursor;
import com.arangodb.ArangoDatabase;
import com.arangodb.entity.BaseDocument;
import com.arangodb.entity.CollectionEntity;
import com.arangodb.entity.IndexEntity;
import com.arangodb.util.MapBuilder;
import com.haizhi.crm.sys.bean.User;
import com.haizhi.crm.sys.bean.UserQo;
import com.haizhi.crm.sys.dao.UserDao;
import com.haizhi.crm.sys.service.IUserService;
import org.apache.logging.log4j.core.impl.ReusableLogEventFactory;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

@Controller
public class UserController {


    @Autowired
    private IUserService userService;

    @Autowired
    private ArangoDatabase adb;

    @ResponseBody
    @RequestMapping(value = "/login")
    public String login(User user) {
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
        try {
            subject.login(token);
        } catch (Exception e) {
            e.printStackTrace();
            return "失败";
        }
        return "成功";
    }

    @ResponseBody
    @RequestMapping(value = "/queryUser")
    public String queryUser(UserQo userQo) {
        return JSON.toJSONString(userService.queryUser(userQo));
    }

    @ResponseBody
    @RequestMapping(value = "/arango/createcoll")
    public String createcoll() {

        if (adb.collection("adb_coll") == null) {
            CollectionEntity collectionEntity = adb.createCollection("adb_coll");
            System.out.println("创建集合:" + collectionEntity.getId() + " : " + collectionEntity.getName());
        } else {
            System.out.println("集合已经存在");
        }

        return "成功";
    }

    @ResponseBody
    @RequestMapping(value = "/arango/adddoc")
    public String adddoc() {

        ArangoCollection coll = adb.collection("adb_coll");

        BaseDocument document = new BaseDocument();
        document.setKey("myKey");
        document.addAttribute("id", 02);
        document.addAttribute("name", "Demo");
        document.addAttribute("tag", "100");

        coll.insertDocument(document);

        return "成功";
    }


    @ResponseBody
    @RequestMapping(value = "/arango/querydoc")
    public Object querydoc() {

        ArangoCollection coll = adb.collection("adb_coll");

        BaseDocument document = coll.getDocument("myKey", BaseDocument.class);
        System.out.println(document.getAttribute("name"));

        Collection<IndexEntity> indexes = coll.getIndexes();


        return indexes;

    }

    @ResponseBody
    @RequestMapping(value = "/arango/aql")
    public Object aql() {

        String query = "FOR t IN adb_coll FILTER t.name == @name RETURN t";

        Map<String, Object> bindVars = new MapBuilder().put("name", "Demo").get();

        ArangoCursor<BaseDocument> cursor = adb.query(query, bindVars, null,
                BaseDocument.class);

        System.out.println("总记录数:" + cursor.getCount());

        int i = 0;
        List<Object> list = new ArrayList<Object>();

        while (cursor.hasNext()) {
            i++;
            BaseDocument baseDocument = cursor.next();
            System.out.println(i + ":" + JSON.toJSONString(baseDocument.getProperties()));
            list.add(baseDocument.getProperties());
        }


        return list;
    }


    @ResponseBody
    @RequestMapping(value = "/query/optimizedFind")
    public Object optimizedFind(String id) {
        return userService.optimizedFind(id);
    }

    @ResponseBody
    @RequestMapping(value = "/query/find")
    public Object find(String id) {
        return userService.find(id);
    }


    /**
     * @param userQo
     * @return
     * @RequestParam(value = "roleNames[]") String[] roleNames
     */
    @ResponseBody
    @RequestMapping(value = "/findUser")
    public Object findUser(UserQo userQo) {

        userQo.setRoleNames(new String[]{"行长"});

        return userService.findUser(userQo);
    }
}
